
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
// On an arduino ESP8266:   D2(SDA)GPIO4,  D1(SCL)GPIO5, ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define BUTTON1 2 //定义输入脚位
#define BUTTON2 3 //定义输入脚位
#define BUTTON3 4 //定义输入脚位

int temp_no = 0;
int led[] = {9, 10, 11, 12}; //led
String ch[] = {"ch1", "ch2", "ch3", "ch4"}; //选择
int led_state[] = {0, 0, 0, 0}; //led数出状态
bool runif = false; //
String runstate = "";
void buttoninfo(int temp) {
  display.clearDisplay();

  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0, 0);            // Start at top-left corner
  Serial.println(temp);
  for (int x = 0; x <= 3; x++) {
    display.print("[");
    if (x == temp) {
      display.print("+"); //如果X=丢过来的值
    }
    else {
      display.print(" ");
    }
    if (led_state[x] == 1) {
      runstate = " <on";
    }
    else {
      runstate = "";
    }
    display.println("]" + String(ch[x]) + runstate);
  }


  display.display();

}

void setup() {
  Serial.begin(9600);
  // 按钮初始化

  for (int x = 0 ; x <= 3; x++) {
    pinMode(led[x],  OUTPUT);
    digitalWrite (led[x], LOW);
  }

  pinMode(BUTTON1, INPUT_PULLUP);
  pinMode(BUTTON2, INPUT_PULLUP);
  pinMode(BUTTON3, INPUT_PULLUP); //确认和重置


  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds
  buttoninfo(temp_no);
  delay(1000);
  // Clear the buffer
  display.clearDisplay();
}

void loop() {
  if (digitalRead(BUTTON1) == 0) {
    if (temp_no == 0)
    {
      temp_no = 0;
    }
    else {
      temp_no --;
    }
  }
  if (digitalRead(BUTTON2) == 0) {
    if (temp_no < 3)
    {
      temp_no ++;//temp_no = temp_no +1
    }
    else {
      temp_no = 3;
    }
  }


  if (digitalRead(BUTTON3) == 0) {

    Serial.println("button 3 on");
    runif =  true;

  }


  if (runif ) {
    delay(300);
    if (digitalRead(BUTTON3) == 0) {
      for (int x = 0 ; x <= 3; x++)
      {
        led_state[x] = 0;
        digitalWrite (led[x], LOW);
      }
      runif  = false;
    }

    else {
      for (int x = 0 ; x <= 3; x++)
      {
        if (x == temp_no) {
          led_state[x] = 1;
          digitalWrite (led[x], HIGH);
          //runstate = " <on";
        }
        else {
          led_state[x] = 0;
          digitalWrite (led[x], LOW);
          //runstate = "";
        }
      }

      runif  = false;

    }
  }



  buttoninfo(temp_no);
  delay(100);
}
